package com.example.nophase.common;

import com.google.gson.JsonSyntaxException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Title: BaseCallBack
 * @Package: com.moomking.mogu.client.framework.http
 * @Description: 为了简化 开发流程 准备的 Callback
 * @date: 2019/5/20
 * @author: liuyunfeng
 * modify shili by 2020-4 去掉了MVP模式
 * @email: liquor6@163.com
 */
public abstract class BaseCallBack<T extends BaseResp> implements Callback<T> {

    @Override
    public void onResponse(Call<T> call, Response<T> response) {

        System.out.println("HttpCallBack:========================请求数据:" + response.toString());
        if (response.code() == 200) {//这个200主要是判断接口是否连接 由于后台返回body中有code字段定义故此字段没有实际意义
            if (response.body() != null) {
                if (response.body().getSuccess()) {
                    try {
                        System.out.println("HttpCallBack:========================请求成功:" + response.body().toString());
                        onSuccess(response);
                    } catch (Exception e) {
                        e.printStackTrace();
                        onFailure(call, new JsonSyntaxException("请求服务器失败:" + response.body().toString()));
                    }
                } else {
                    onFailure(call, new UnknownError("请求失败:" + response.body().getMsg()));
                }
            } else if (response.body() == null) {
                onFailure(call, new NullPointerException("空指针异常:" + response.code()));
            }
        }else if(response.code() == 401){
            //TODO 此处要增加 弹出窗口消息 或者提示重新登录等等
            onFailure(call, new ConnectException("服务器权限超时:" + response.code()));
        }else {
            onFailure(call, new ConnectException("访问服务器出错:" + response.code()));
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        if (t instanceof SocketTimeoutException) {
            System.out.println("========================Socket超时:" + t.getMessage());
        } else if (t instanceof ConnectException) {
            System.out.println("========================连接异常:" + t.getMessage());
        } else if (t instanceof JsonSyntaxException) {
            System.out.println("========================json格式异常:" + t.getMessage());
        } else if (t instanceof NullPointerException) {
            System.out.println("========================空指针异常:" + t.getMessage());
        } else if (t instanceof RuntimeException) {
            System.out.println("========================运行时异常:" + t.getMessage());
        } else if (t instanceof UnknownError) {
            System.out.println("========================服务器异常:" + t.getMessage());
        } else {
            System.out.println("========================其他异常:" + t.getMessage());
        }
        onFailure(t.getMessage());
    }

    public abstract void onSuccess(Response<T> response) throws Exception;

    public abstract void onFailure(String message);
}